package com.zs.server;

import com.zs.handler.ServerHandler;
import com.zs.servlet.Myservlet;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.List;
import java.util.concurrent.*;

public class TomcatServer {
    private static int port = 6000;
    //servlet容器，存储请求对应的Servlet
    public static final ConcurrentHashMap<String, Myservlet> servletMap = new ConcurrentHashMap<String, Myservlet>();
    //servlet对应的uri
    public static final ConcurrentHashMap<String, String> uriMap = new ConcurrentHashMap<String, String>();


    public static void main(String[] args) {
        init();
        run();
    }

    //读取xml配置文件,初始化servlet容器
    private static void init() {
        //获取web.xml目录地址
        String path = TomcatServer.class.getResource("/WEB-INFO").getPath();
        //获取解析器
        SAXReader saxReader = new SAXReader();
        try {
            Document document = saxReader.read(path + "/web.xml");
            //获取xml文件根标签,遍历根标签下所有字标签
            Element rootElement = document.getRootElement();
            List<Element> elements = rootElement.elements();
            for (Element element : elements) {
                if ("servlet".equalsIgnoreCase(element.getName())) {
                    //获取字标签内容
                    Element servletName = element.element("servlet-name");
                    Element servletClass = element.element("servlet-class");
                    System.out.println("servlet名称是：" + servletName.getText() +" ，servlet类型是：" + servletClass.getText());
                    //将servlet实例存入servlet容器
                    servletMap.put(servletName.getText(), (Myservlet) TomcatServer.class.getClassLoader().loadClass(servletClass.getText()).newInstance());
                } else if ("servlet-mapping".equalsIgnoreCase(element.getName())) {
                    //获取字标签内容
                    Element servletName = element.element("servlet-name");
                    Element urlPattern = element.element("url-pattern");
                    System.out.println("servlet名称是：" + servletName.getText() +" ，url是：" + urlPattern.getText());
                    //将servlet实例存入servlet容器
                    uriMap.put(urlPattern.getText(),servletName.getText());
                }
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    //启动tomcat
    private static void run() {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(500, 500, 20, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(20000));
        try {
            ServerSocket serverSocket = new ServerSocket(port);
            System.out.println("服务启动");
            while (!serverSocket.isClosed()) {
                Socket socket = serverSocket.accept();
                threadPoolExecutor.execute(new ServerHandler(socket));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
